package com.example.tuike.controller;

import com.example.tuike.entity.Feeds;
import com.example.tuike.service.FeedsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/*** @Author supersaiya
 * @Description TODO

 * @Date 2025/2/2 21:27

 * @Version 1.0*/


@RestController
@RequestMapping("/api/feeds")
public class FeedsController {

    @Autowired
    private FeedsService feedsService;

    @GetMapping
    public ResponseEntity<List<Feeds>> getAllFeeds() {
        List<Feeds> feeds = feedsService.getAllFeeds();
        return ResponseEntity.ok(feeds);
    }

    @GetMapping("/{id}")
    public ResponseEntity<Feeds> getFeedById(@PathVariable Integer id) {
        Feeds feed = feedsService.getFeedById(id);
        if (feed == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(feed);
    }

    @PostMapping
    public ResponseEntity<Feeds> createFeed(@RequestBody Feeds feed) {
        Feeds createdFeed = feedsService.createFeed(feed);
        return ResponseEntity.ok(createdFeed);
    }

    @PutMapping("/{id}")
    public ResponseEntity<Feeds> updateFeed(@PathVariable Integer id, @RequestBody Feeds feed) {
        feed.setFeedId(id);
        Feeds updatedFeed = feedsService.updateFeed(feed);
        return ResponseEntity.ok(updatedFeed);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteFeed(@PathVariable Integer id) {
        feedsService.deleteFeed(id);
        return ResponseEntity.noContent().build();
    }
}
